﻿
using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Servers
{
    public class SysCodeServers
    {
	    private readonly IRepository<sys_code> sys_codeRepository;

        public SysCodeServers()
        {
            sys_codeRepository = new DapperRepository<sys_code>();

        }

        public List<sys_code> GetData()
        {
            return sys_codeRepository.GetAllEnumerable().ToList();
        }

        public sys_code GetModelById(string id)
        {
            return sys_codeRepository.GetById(id);
        }

		
        internal void Add(sys_code model)
        {
            sys_codeRepository.Insert(model);
        }

        internal void Update(sys_code model)
        {
            sys_codeRepository.Update(model);
        }

		internal void Del(string id)
        {
            sys_codeRepository.DeleteByIds(id);
        }

				      
        internal List<SelectListItem> GetPARENT_NO()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = sys_codeRepository.SqlWithParamsDynamic("select a.CODE_ID Value,concat(a.CODE_NAME,'-----',a.CODE_NO) Text from sys_code  a  where PARENT_NO=0");
            tables.Clear();
            tables.Add(new SelectListItem { Value = "0", Text = "代码主分类" });
            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }

        internal List<sys_code> GetData(string cODE_NO, string cODE_NAME, string pARENT_NO)
        {
            var dstable = sys_codeRepository.GetAllEnumerable();
            if (!string.IsNullOrEmpty(cODE_NO))
            {
                dstable= dstable.Where(m => m.CODE_NO == cODE_NO);
            }
            if (!string.IsNullOrEmpty(cODE_NAME))
            {
                dstable= dstable.Where(m => m.CODE_NAME.Contains(cODE_NAME));
            }
            if (!string.IsNullOrEmpty(pARENT_NO))
            {
                dstable= dstable.Where(m => m.PARENT_NO==int.Parse(pARENT_NO));
            }
            return dstable.ToList();
        }
    }
}